package com.example.computer_component_manager.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.example.computer_component_manager.R
import com.example.computer_component_manager.adapter.BrandAdapter
import com.example.computer_component_manager.bean.AllGpuInfo
import com.example.computer_component_manager.bean.Brand
import com.example.computer_component_manager.bean.Brands
import com.example.computer_component_manager.databinding.ActivityGpuEditorBinding
import com.example.computer_component_manager.fragment.impl.GpuFragment
import com.example.computer_component_manager.util.DbUtil
import com.example.computer_component_manager.util.ToastUtil
import com.example.computer_component_manager.volley.MyJsonObjectRequest
import com.example.computer_component_manager.volley.VolleyApplication
import org.json.JSONObject
import java.math.BigDecimal

/**
 * GPU编辑器(Activity)
 *
 * @author 1923111015董寺卓
 */

class GpuEditorActivity : AppCompatActivity() {

    private lateinit var binding: ActivityGpuEditorBinding

    // 显卡信息默认值
    private var gpu = AllGpuInfo.GpuInfo(
        0, Brand(1, ""), "", 1, BigDecimal(0), 0, "")

    // 品牌位置
    private var brandPosition = 0

    // 品牌集合
    private lateinit var brands: Brands

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityGpuEditorBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val gpuItemPosition = intent.getIntExtra("gpuItemPosition", -1)
        if (gpuItemPosition > -1) {
            gpu = GpuFragment.allGpuInfo[gpuItemPosition]
        }

        // 品牌信息
        brands = DbUtil.gson.fromJson(intent.getStringExtra("gpuBrands"), Brands::class.java)
        binding.spinnerBrand.adapter = BrandAdapter(this, brands)
        // 选中品牌
        if (gpuItemPosition > -1) {
            for ((index, e) in brands.withIndex()) {
                if (e.id == gpu.brand.id)
                    binding.spinnerBrand.setSelection(index)
            }
        }
        binding.etGpuEditorType.setText(gpu.type)
        binding.etGpuEditorPrice.setText(gpu.consumerPrice.toDouble().toString())
        binding.etGpuEditorImage.setText(gpu.imgUrl)
        binding.etGpuEditorVideoMemory.setText(gpu.size.toString())
        binding.spinnerBrand.onItemSelectedListener =
            object : AdapterView.OnItemSelectedListener {

                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View,
                    position: Int,
                    id: Long,
                ) {
                    binding.spinnerBrand.setSelection(position)
                    this@GpuEditorActivity.brandPosition = position
                    Log.d("Select", binding.spinnerBrand.selectedItemPosition.toString())
                }

                override fun onNothingSelected(parent: AdapterView<*>?) {
                    Toast.makeText(this@GpuEditorActivity,
                        resources.getString(R.string.nothing_selected),
                        Toast.LENGTH_SHORT).show()
                }
            }
    }

    /**
     * 预览图片方法
     */
    fun showImage(view: View) {
        if (view.id == R.id.btnGpuEditorImageView) {
            Glide.with(this).load(binding.etGpuEditorImage.text.toString())
                .into(binding.ivGpuGpuEditorImageView)
        }
    }

    /**
     * 提交方法
     */
    fun submit(view: View) {
        if (view.id == R.id.btnCpuEditorSubmit) {
            val gpu = AllGpuInfo.GpuInfo(gpu.id, brands[brandPosition],
                binding.etGpuEditorType.text.toString(),
                binding.etGpuEditorVideoMemory.text.toString().toByte(),
                BigDecimal(binding.etGpuEditorPrice.text.toString()),
                gpu.stock, binding.etGpuEditorImage.text.toString())
            val reqJsonObject = JSONObject(DbUtil.gson.toJson(gpu))
            val myJsonObjectRequest =
                MyJsonObjectRequest(DbUtil.UPDATE, "${DbUtil.API_PREFIX}/gpu/${gpu.id}",
                    reqJsonObject, {
                        Log.d("TAG", "submit: $it")
                        this.setResult(RESULT_OK)
                        finish()
                    }, {
                        ToastUtil.showShortError(this)
                        this.setResult(RESULT_CANCELED)
                    })
            VolleyApplication.instance.requestQueue.add(myJsonObjectRequest)
        }

    }
}

